import { createApp, h, inject } from "vue";

import App from "./App.vue";

import {
  createWebHashHistory,
  createMemoryHistory,
  createRouter,
  createWebHistory,
} from "vue-router";

import User from "./views/User.vue";
import Home from "./views/Home.vue";
import Login from "./views/Login.vue";
import Shop from "./views/Shop.vue";
import Body from "./views/Body.vue";
import Header from "./views/Header.vue";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/home",
      component: Home,
      beforeEnter(to, from) {
        console.log("Home beforeEnter");
        if (from.fullPath === "/user") {
          // return false;
          // return "/login";
          // throw new Error("error home");
        }
      },
    },
    {
      path: "/shop",
      component: Shop,
    },
    {
      path: "/user",
      component: User,
    },
    {
      path: "/login",
      name: "login",
      component: Login,
    },
  ],
});

router.beforeEach((to, from) => {
  console.log("beforeEach");
});
router.beforeResolve((to, from) => {
  console.log("beforeResolve");
});

router.afterEach((to, from) => {
  console.log("afterEach");
});
// 捕捉错误
router.onError((err) => {
  console.log(err.message);
});

const app = createApp({ render: () => h(App) });

app.use(router);

app.mount("#app");
